package com.houtai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.entity.EduLog;
import com.houtai.eduservice.entity.vo.LogQuery;
import com.houtai.eduservice.service.EduLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/eduservice/log")
public class EduLogController {
    @Autowired
    private EduLogService logService;
    //4.多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("pageLogCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable long current,
                                       @PathVariable long limit,
                                       @RequestBody(required = false) LogQuery logQuery){
        //创建page对象
        Page<EduLog> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<EduLog> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String username=logQuery.getUserName();
        String begin = logQuery.getBegin();
        String end = logQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(username)){
            //构建条件
            wrapper.like("username",username);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("create_time",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("create_time",end);

        }
        //排序
        wrapper.orderByDesc("create_time");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        logService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<EduLog> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }

}

